﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public class ToolDataMgr
    {
        private static ToolDataMgr instance;
        private static object _lock = new object();

        private ToolDataMgr()
        {

        }

        public static ToolDataMgr GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new ToolDataMgr();
                    }
                }
            }
            return instance;
        }

        #region 工具数据
        
        // 获取工具列表
        public DataTable GetToolListDataTable()
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.*, b.f_name as f_tooltypename, b.f_picture, c.f_name as f_posname " +
                "from TS_Tool_Tools as a " +
                "left join TS_Tool_Type as b on a.f_typeid=b.f_id " +
                "left join TS_Tool_Pos as c on a.f_position=c.f_id " +
                "where a.f_del=0 ";

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 获取不在工具箱内的工具
        public DataTable GetToolListNobox()
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.*, b.f_name as f_tooltypename, c.f_name as f_posname " +
                "from TS_Tool_Tools as a " +
                "left join TS_Tool_Type as b on a.f_typeid=b.f_id " +
                "left join TS_Tool_Pos as c on a.f_position=c.f_id " +
                "where a.f_del=0 and a.f_parentid=0 and a.f_toolbox=0";

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 获取工具箱以及工具箱内的工具
        public DataTable GetToolListBoxAndTool()
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.*, b.f_name as f_tooltypename, c.f_name as f_posname " +
                "from TS_Tool_Tools as a " +
                "left join TS_Tool_Type as b on a.f_typeid=b.f_id " +
                "left join TS_Tool_Pos as c on a.f_position=c.f_id " +
                "where a.f_del=0 and (a.f_parentid>0 or a.f_toolbox=1)";

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        public DataTable GetToolByPos(int iPosID)
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.*, b.f_name as f_tooltypename " +
                "from TS_Tool_Tools as a " +
                "left join TS_Tool_Type as b on a.f_typeid=b.f_id " +
                "where a.f_del=0 and a.f_parentid=0 and a.f_position=" + iPosID;

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 添加工具
        public bool AddTool(TS_Tool oTool)
        {
            try
            {
                string strSQL = "insert into TS_Tool_Tools(f_typeid, f_toolbox, f_rfid, f_position, f_parentid, f_statetype, f_fcid, f_fcprice, f_fcdate, f_fccode) values('"
                    + oTool.ITypeID + "','"
                    + oTool.IToolBox + "','"
                    + oTool.SRfID + "','"
                    + oTool.IPosID + "','"
                    + oTool.IParentID + "','"
                    + oTool.IStatusType + "','"
                    + oTool.SFactoryID + "','"
                    + oTool.FPrice + "','"
                    + oTool.OFcDate.ToString("yyyy/MM/dd hh:mm:ss") + "','"
                    + oTool.SFcCode + "')";

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            
            return true;
        }

        // 修改工具
        public bool ModifyTool(TS_Tool oTool)
        {
            try
            {
                string strSQL = "update TS_Tool_Tools set " +
                    "f_typeid='" + oTool.ITypeID.ToString() +
                    "',f_rfid='" + oTool.SRfID +
                    "',f_position='" + oTool.IPosID.ToString() +
                    "',f_parentid='" + oTool.IParentID +
                    "',f_statetype='" + oTool.IStatusType +
                    "',f_fcid='" + oTool.SFactoryID.ToString() +
                    "',f_fcprice='" + oTool.FPrice +
                    "',f_fcdate='" + oTool.OFcDate.ToString("yyyy/MM/dd hh:mm:ss") +
                    "',f_fccode='" + oTool.SFcCode +
                    "' where f_id = " + oTool.IID.ToString();
                
                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 修改工具的父对象
        public bool ModifyToolParent(int iToolID, int iParentID)
        {
            try
            {
                string strSQL = "update TS_Tool_Tools set " +
                    "f_parentid=" + iParentID +
                    " where f_id = " + iToolID;

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 修改工具的父对象
        public bool ModifyToolStatus(int iToolID, int iStatus)
        {
            try
            {
                string strSQL = "update TS_Tool_Tools set " +
                    "f_statetype=" + iStatus +
                    " where f_id = " + iToolID;

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 删除工具
        public bool DeleteTool(int iToolID)
        {
            try
            {
                string strSQL = "update TS_Tool_Tools set f_del=1 where f_id = " + iToolID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 判断RFID是否已存在，已存在则返回存在的工具id
        public int IsRFIDExist(string sRFID)
        {
            int iID = 0;

            try
            {
                DataTable dt = new DataTable();

                string strSQL = "select * from TS_Tool_Tools where f_del=0 and f_rfid='" + sRFID.Trim() + "'";

                TS_Win_Class.FilledDataTable(strSQL, dt);
                if (dt.Rows.Count > 0)
                {
                    iID = int.Parse(dt.Rows[0]["f_id"].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return iID;
        }
        #endregion

        #region 工具型号数据
        // 获取所有工具型号
        public DataTable GetToolTypeDataTable()
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_Tool_Type where f_del=0";
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 获取所有工具型号叶子节点
        public DataTable GetLeafToolType()
        {
            DataTable dt = new DataTable();
            string strSQL = "select* from TS_Tool_Type where f_id not in (select distinct f_parentid from TS_Tool_Type) and f_del = 0";
            TS_Win_Class.FilledDataTable(strSQL, dt);
            
            return dt;
        }

        // 添加工具型号
        public bool AddToolType(TS_ToolType oType)
        {
            try
            {
                string strSQL = "insert into TS_Tool_Type(f_name, f_repaircycle, f_picture, f_del) values('"
                    + oType.SName + "','"
                    + oType.IRepairCycle.ToString() + "','"
                    + oType.SPicture + "',0)";

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            
            return true;
        }

        // 添加工具型号
        public bool ModifyToolType(TS_ToolType oType)
        {
            try
            {
                string strSQL = "update TS_Tool_Type set " +
                    "f_name='" + oType.SName +
                    "',f_repaircycle='" + oType.IRepairCycle.ToString() +
                    "',f_picture='" + oType.SPicture +
                    "' where f_id = " + oType.IID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 添加工具型号
        public bool DelToolType(int iTypeID)
        {
            try
            {
                string strSQL = "update TS_Tool_Type set f_del=1 where f_id = " + iTypeID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 修改工具类型父对象
        public bool ModifyToolTypeParent(int iID, int iParentID)
        {
            try
            {
                string strSQL = "update TS_Tool_Type set " +
                    "f_parentid=" + iParentID +
                    " where f_id = " + iID;

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }
        #endregion

        #region 工具位置数据
        public DataTable GetToolPosDataTable()
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_Tool_Pos where f_del=0";

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        public DataTable GetLeafToolPos()
        {
            DataTable dt = new DataTable();
            string strSQL = "select* from TS_Tool_Pos where f_id not in (select distinct f_parentid from TS_Tool_Pos) and f_del = 0";
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 根据
        public DataTable GetToolPosChildren(int iParentID)
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_Tool_Pos where f_del=0 and f_parentid=" + iParentID;

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 添加工具位置
        public bool AddToolPos(TS_ToolPos oToolPos)
        {
            try
            {
                string strSQL = "insert into TS_Tool_Pos(f_name, f_parentid, f_del) values('"
                    + oToolPos.SName + "','"
                    + oToolPos.IParentID.ToString() + "',0)";

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 添加工具位置
        public bool ModifyToolPos(TS_ToolPos oToolPos)
        {
            try
            {
                string strSQL = "update TS_Tool_Pos set " +
                    "f_name='" + oToolPos.SName +
                    "' where f_id = " + oToolPos.IID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 添加工具位置
        public bool DeleteToolPos(int iTypeID)
        {
            try
            {
                string strSQL = "update TS_Tool_Pos set f_del=1 where f_id = " + iTypeID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }
        #endregion

        #region 工单数据
        // 获取订单数据
        public DataTable GetOrderList(int iType)
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.*, b.f_name as f_proposname, c.f_name as f_sendname, d.f_name as f_recivename " +
                "from TS_Tool_Order as a " +
                "left join TS_S_T_P_Stuff as b on a.f_proposid=b.f_workercode " +
                "left join TS_S_T_P_Stuff as c on a.f_sendid=c.f_workercode " +
                "left join TS_S_T_P_Stuff as d on a.f_reciveid=d.f_workercode " +
                "where a.f_del=0 ";

            if (iType > 0)
            {
                strSQL = strSQL + " and f_status=" + iType.ToString();
            }

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 修改订单
        public bool ModifyOrder(TS_Order order)
        {
            try
            {
                string strSQL = "update TS_Tool_Order set " +
                    "f_proposid='" + order.IProposID +
                    "', f_propostime='" + Utility.GetDateTime(order.SProposTime) +
                    "', f_sendid='" + order.ISendID +
                    "', f_reciveid='" + order.IReciveID;

                if (order.SSendTime != DateTime.MinValue)
                    strSQL = strSQL + "', f_sendtime='" + Utility.GetDateTime(order.SSendTime);

                if (order.SReciveTime != DateTime.MinValue)
                    strSQL = strSQL + "', f_recivetime='" + Utility.GetDateTime(order.SReciveTime);

                strSQL = strSQL + "', f_status='" + (int)order.IStatus +
                    "' where f_id = " + order.IID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 修改订单状态
        public bool ModifyOrderStatus(int iID, int iStatus)
        {
            try
            {
                string strSQL = "update TS_Tool_Order set " +
                    "f_status='" + iStatus.ToString() +
                    "' where f_id = " + iID.ToString();

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                return false;
            }

            return true;
        }

        // 获取订单详情
        public DataTable GetOrderDetail(int iID)
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.f_orderid, a.f_toolid, b.f_statetype, b.f_rfid, c.f_name, d.f_name as f_posname " +
                "from TS_Tool_OrderDetail as a " +
                "left join TS_Tool_Tools as b on a.f_toolid=b.f_id " +
                "left join TS_Tool_Type as c on b.f_typeid=c.f_id " +
                "left join TS_Tool_Pos as d on b.f_position=d.f_id " +
                "where a.f_del=0 and a.f_orderid=" + iID.ToString();
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        #endregion

        #region 修程匹配数据
        // 获取所有机型修程
        public DataTable GetTrainRepairTable()
        {
            DataTable dt = new DataTable();

            string strSQL = "select a.f_id, a.f_name, c.f_id as f_rpid, c.f_name as f_rpname from TS_S_T_P_TrainType as a " +
                " inner join TS_S_T_P_Train_RP_TC as b on b.f_tcid=a.f_tcid " +
                " inner join TS_S_T_P_Train_RP as c on c.f_id=b.f_rpid " +
                " where a.f_del=0";

            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 获取所有修程数据
        public DataTable GetRepairMatchDataTable(int iTrainType, int iRepair)
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_Tool_TrainRepair where f_del=0 " +
                " and f_traintypeid=" + iTrainType +
                " and f_rpid=" + iRepair;
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 添加工具型号
        public bool AddRepairMatch(int iTrainType, int iRepair, int iToolType)
        {
            try
            {
                string strSQL = "insert into TS_Tool_TrainRepair(f_traintypeid, f_rpid, f_tooltypeid) values('"
                    + iTrainType.ToString() + "','"
                    + iRepair.ToString() + "','"
                    + iToolType.ToString() + "')";

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }

        // 添加工具型号
        public bool DelRepairMatch(int iTrainType, int iRepair, int iToolType)
        {
            try
            {
                string strSQL = "update TS_Tool_TrainRepair set f_del=1 where f_traintypeid = " + iTrainType +
                    " and f_rpid=" + iRepair +
                    " and f_tooltypeid=" + iToolType;

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }
        #endregion

        #region 班组匹配数据
        // 获取所有班组信息
        public DataTable GetAllGroup()
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_S_T_P_Department where f_del=0";
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 获取所有修程数据
        public DataTable GetGroupMatchDataTable(int iGroup)
        {
            DataTable dt = new DataTable();

            string strSQL = "select * from TS_Tool_Group where f_del=0 and f_groupid=" + iGroup;
            TS_Win_Class.FilledDataTable(strSQL, dt);

            return dt;
        }

        // 添加工具型号
        public bool AddGroupMatch(int iGroupID, int iToolTypeID)
        {
            try
            {
                string strSQL = "insert into TS_Tool_Group(f_groupid, f_tooltypeid) values('"
                    + iGroupID.ToString() + "','"
                    + iToolTypeID.ToString() + "')";

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }


        // 添加工具型号
        public bool DelGroupMatch(int iGroupID, int iToolTypeID)
        {
            try
            {
                string strSQL = "update TS_Tool_Group set f_del=1 where f_groupid = " + iGroupID 
                    + " and f_tooltypeid=" + iToolTypeID;

                TS_Win_Class.MySqlCom(strSQL);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

            return true;
        }
        #endregion

    }
}
